﻿<%@ Page Language="C#" MasterPageFile="~/Pages/Base/ListItemBase.master" CodeFile="~/Pages/Base/ListItemBase.cs" Inherits="ListItemBasePage" %>

<script runat="server">
    
    // 数据库交互对象
    private StateGrid.User.UserManageDAO dao;

    private IList<StateGrid.User.User> data;

    private StateGrid.User.User login;

    protected override void DoInit()
    {
        this.dao = new StateGrid.User.UserManageDAO();

        //判断用户是否登录
        this.login = (StateGrid.User.User)Session["login"];
        if (null == this.login)
        {
            StateGrid.GlobalMethod.RedirectLogin(Response);
        }
    }

    protected override void SetItemFields()
    {
        RegisterKeyField("name");
        
        AddBoudField("用户名", "name", 0,25);
        AddBoudField("限制IP", "ipLimit", 0,40);
        AddBoudField("角色", "role", 0,20);
        
        AddHyperLinkField("编辑",
                          StateGrid.GlobalVariables.GlobalUser.EDITUSER_PAGE + "?name={0}",
                          new string[] {"name"});
    }

    protected override int GetItemCount()
    {
        return dao.CountUsers()-1;
    }

    protected override object GetItemSource(int begin, int end)
    {
        data = dao.GetSomeUsers(begin, end,StateGrid.User.UserManageDAO.UserOrderType.NONE);       
        StateGrid.User.User user = null;
        if (null != data)
        {
            foreach (StateGrid.User.User temp in data)
            {
                if (temp.name.Equals("admin"))
                {
                    user = temp;
                    break;
                }
            }

            if (null != user)
            {
                data.Remove(user);
            }
        }   
        return data;
    }

    protected override bool OnDelete(DataKey dataKey, out string message)
    {
        Boolean ret = dao.DeleteUserByName(dataKey.Value as string);
        if (ret)
        {
            message = "删除成功" ;
            return true;
        }

        message = "删除失败";
        return false;
    }

    protected override Boolean HasDeleteField()
    {
        return true;
    }
</script>
